import { Component, OnInit, TemplateRef } from '@angular/core';
import { NbDialogService, NbWindowRef, NbWindowService } from '@nebular/theme';
import { CheckWeekdayComponent } from '../check-weekday/check-weekday.component';
import { HttpHelper } from '../../Helper/HttpHelper';
import { DtoResultObj } from 'src/app/Model/DtoRec/DtoResult';
import { LocalDataSource } from 'src/app/Lib/ng2-smart-table/public-api';
import { DtoSaveObj } from 'src/app/Model/DtoPost/DtoSaveObj';
import { Fun } from 'src/app/Config/Fun';

@Component({
  selector: 'app-course-change',
  templateUrl: './course-change.component.html'
})
export class CourseChangeComponent implements OnInit {
  buttons = []
  courseDic: any = null;
  bean = {
    week_course_id1: 0,
    week_course_id2: 0,
    changeDateStr1: null,
    changeDateStr2: null,
  };
  classes_id = 0;
  screenheight: number = document.documentElement.clientHeight - 300
  screenWidth: number = document.documentElement.clientWidth * 0.8
  changeMsg: any;
  constructor(
    public httpHelper: HttpHelper,
    public windowRef: NbWindowRef
  ) {

  }

  ngOnInit() {

  }

  myModelChange(val) {
    if (this.classes_id != val) {
      this.classes_id = val;
      console.log(this.classes_id);
      this.httpHelper.Post("WeekCourse/GetClassesCourse", { Key: this.classes_id }).then((result: DtoResultObj<any>) => {
        Fun.HideLoading();
        if (result.success) {
          this.courseDic = result.data.courseDic
          console.log(this.courseDic)
        }
        else {
          Fun.Hint(result.msg);
        }
        console.log(result);
      })
    }
  }

  openObj;
  changeList: Array<any>;
  clickCourse(id, dialog: TemplateRef<any>) {
    if (this.changeList == null) this.changeList = [];
    if (this.changeList.length > 1) {
      this.changeList.splice(0, 1);
    }
    console.log(this.courseDic[id])
    if (this.courseDic[id].order_num == 0) {
      this.courseDic[id].class_id = this.classes_id;
      this.courseDic[id].order_num = parseInt(id.replace('a', ''));
    }
    this.changeList.push(this.courseDic[id]);
    if (this.changeList.length > 0) this.bean.week_course_id1 = this.changeList[0].week_course_id;
    if (this.changeList.length > 1) this.bean.week_course_id2 = this.changeList[1].week_course_id;

  }
  CheckWeekCourseChange() {
    console.log(this.bean);
    if(this.bean.changeDateStr1==null || this.bean.changeDateStr2==null){
      alert("交换时间不能为空")
      return
    }
    if("object"==typeof this.bean.changeDateStr1) this.bean.changeDateStr1 = this.bean.changeDateStr1.toDateString();
    if("object"==typeof this.bean.changeDateStr2) this.bean.changeDateStr2 = this.bean.changeDateStr2.toDateString();

    this.httpHelper.Post("WeekCourseChange/CheckWeekCourseChange", this.bean).then((result: DtoResultObj<string>) => {
      Fun.HideLoading();
      if (result.success) {
        this.changeMsg = result.dataList.join("\r\n");
      }
      else {
        Fun.Hint(result.msg);
      }
      console.log(result);
    })
  }

  ButtonClick(even) {
    console.log(this.bean);
    if(this.bean.changeDateStr1==null || this.bean.changeDateStr2==null){
      alert("交换时间不能为空")
      return
    }
    if("object"==typeof this.bean.changeDateStr1) this.bean.changeDateStr1 = this.bean.changeDateStr1.toDateString();
    if("object"==typeof this.bean.changeDateStr2) this.bean.changeDateStr2 = this.bean.changeDateStr2.toDateString();

    even(this.bean)
      .then((x: DtoResultObj<any>) => {
        console.log(x)
        if (x.success) {
          this.windowRef.close();
        }
      })
  }

  /**
   * 交换课程
   */
  ChangeCourse() {
    let postEnt = {
      Key: []
    }
    this.changeList.forEach(element => {
      console.log(element.order_num)
      var tmp1 = element.order_num / 10;
      var tmp2 = element.order_num % 10;
      postEnt.Key.push({
        k: element.classes_name,
        v: tmp2 + "" + Math.floor(tmp1)
      });
    });

    console.log(postEnt);
    this.httpHelper.Post("WeekCourseChange/ChangeWeekCourse", postEnt).then((result: DtoResultObj<string>) => {
      Fun.HideLoading();
      if (result.success) {
        var tmp = this.classes_id;
        this.classes_id = 0;
        this.myModelChange(tmp)
      }
      else {
        Fun.Hint(result.msg);
      }
      console.log(result);
    })
  }

}
